<template>
  <PageLayout>
    <template #extra>
      <div class="bg-fff p-24 mb-24">
        <a-typography-title :level="3">商户数据概况</a-typography-title>
        <div class="bg-fff d-flex j-sb card-list">
          <div>
            <div>{{ detail.totalNum }}</div>
            <div>当前总商户</div>
          </div>
          <div>
            <div>{{ detail.addNum }}</div>
            <div>昨日新增商户</div>
          </div>
          <div>
            <div>{{ detail.disableNum }}</div>
            <div>已禁用商户</div>
          </div>
        </div>
      </div>
    </template>
    <template #header>
      <BasicForm :options="queryOptions" @ok="onQueryDataByParams"></BasicForm>
    </template>
    <div class="table-box">
      <div class="head-box">
        <div class="title">商户列表</div>
        <a-space :size="16">
          <a-button type="primary" @click="edit()">新建</a-button>
          <a-button>数据导出</a-button>
        </a-space>
      </div>
      <div class="bg-fff">
        <!-- :apiFetch="apiFetch"  -->
        <BasicTable
          ref="tableRef"
          :columns="columns"
          :query-params="queryData"
          :apiFetch="postShopUserPage"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'action'">
              <a-space class="space-btn--box" :size="10">
                <a class="btn" @click="edit(record)">修改</a>
                <a class="btn" v-if="!record.status" @click="changeStatus(record)">禁用</a>
                <a class="btn" v-else @click="changeStatus(record)">启用</a>
                <a class="btn" @click="remove(record.id)">删除</a>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </div>
    </div>
  </PageLayout>
</template>

<script name="store-merchant-list" setup>
import {
  postShopUserPage,
  postShopUserDelete,
  postShopUserStatistics,
  postShopUserUpdateStatus
} from "@/api/common";

import { comfirmModal } from "@/hooks/useModal";
import { message } from "ant-design-vue";
const router = useRouter();
const queryOptions = ref([
  {
    name: "商户名",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "realName",
  },
  {
    name: "账号",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "username",
  },
  {
    name: "手机号码",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "phone",
  },
]);
const columns = [
  {
    title: "序号",
    dataIndex: "index",
    width: "80px",
    customRender: (record) => `${record.index + 1}`,
  },
  {
    title: "商户账号",
    dataIndex: "username",
    key: "username",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "商户名",
    dataIndex: "realName",
    key: "realName",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "联系电话",
    dataIndex: "phone",
    key: "phone",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "门店数量",
    dataIndex: "shopNum",
    key: "shopNum",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "设备数量",
    dataIndex: "deviceNum",
    key: "deviceNum",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "身份证号码",
    dataIndex: "idCard",
    key: "idCard",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "角色",
    dataIndex: "roleType",
    key: "roleType",
    width: "150px",
    ellipsis: true,
    customRender: ({ record }) => `${record.roleType == 0 ? "管理员" : "员工"}`,
  },
  {
    title: "状态",
    dataIndex: "status",
    key: "status",
    width: "150px",
    ellipsis: true,
    customRender: ({ record }) => `${!record.status ? "正常" : "禁用"}`,
  },

  {
    title: "操作",
    fixed: "right",
    dataIndex: "action",
    key: "action",
    width: 200,
  },
];
const detail = ref({});
onMounted(() => {
  postShopUserStatistics().then((res) => {
    detail.value = res.data;
  });
});

const queryData = reactive({});
const tableRef = ref(null);
const onQueryDataByParams = (params) => {
  for (let key in params) {
    queryData[key] = params[key];
  }
  tableRef.value.refreshTableData();
};

const edit = (record) => {
  router.push({
    path: "/store/merchant/edit",
    query: {
      id: record?.id,
    },
  });
};
// const exportFile = () => {
//   postShopExport().then((res) => {
//     message.success("导出成功");
//   });
// };

const changeStatus = (record) => {
  const title = !record.status  ? "禁用" : "启用";
  comfirmModal({
    content: `确认${title}该商户`,
    onOk() {
      postShopUserUpdateStatus({
        id: record.id,
        status: !record.status,
      }).then(() => {
        message.success(`${title}成功`);
        tableRef.value.fetch();
      });
    },
  });
};

const remove = (id) => {
  comfirmModal({
    content: "确认删除该商户",
    onOk() {
      postShopUserDelete({ id }).then(() => {
        message.success("删除成功");
        tableRef.value.fetch();
      });
    },
  });
};
</script>

<style lang="less" scoped>
.card-list {
  // margin-bottom: 24px;
  display: flex;
  gap: 50px;
  > div {
    // flex: 1;
    width: 300px;
    padding: 10px;
    // height: 100px;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 30px;
    > div:first-child {
      font-size: 48px;
      font-weight: 600;
    }
    &:nth-child(1) {
      background: #02a7f0;
    }
    &:nth-child(2) {
      background: #66cc99;
    }
    &:nth-child(3) {
      background: #e62129;
    }
  }
}
</style>
